package com.lb.shopguide.ui.fragment.guide.recommend;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lb.baselib.http.ResponseCode;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.http.method.ApiMethodGuide;
import com.lb.shopguide.ui.fragment.FragmentGuide;
import com.lb.shopguide.util.json.JsonUtil;
import com.trello.rxlifecycle2.android.FragmentEvent;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

/**
 * Create by kangzhao on 2018/12/4
 */
public class FragmentPlayVideo extends BaseCommonFragment {
    @BindView(R.id.video_player)
    JzvdStd videoPlayer;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_goto_earn_money)
    TextView tvGotoEarnMoney;

    private String videoUrl;

    public static FragmentPlayVideo newInstance() {
        FragmentPlayVideo fragment = new FragmentPlayVideo();
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_play_video;
    }

    @Override
    public void initData() {
        sendRequestGetMaterial();
    }

    @Override
    public void initView() {
        videoPlayer.setUp(videoUrl, "", Jzvd.SCREEN_WINDOW_NORMAL);
        videoPlayer.setVisibility(View.VISIBLE);
//        videoPlayer.startVideo();
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {

    }

    @Override
    public void requestError(String error) {

    }

    @OnClick(R.id.iv_close)
    void onBack() {
        pop();
    }

    @OnClick(R.id.tv_goto_earn_money)
    void gotoEarnMoney() {
        pop();
    }

    @Override
    public String getPageTag() {
        return FragmentPlayVideo.class.getCanonicalName();
    }

    @Override
    public void onDetach() {
        super.onDetach();
//        videoPlayer.release();

    }

    @Override
    public void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }

    private void playVideo() {
        videoPlayer.setUp(videoUrl, "", Jzvd.SCREEN_WINDOW_NORMAL);
        videoPlayer.startVideo();
    }


    private void sendRequestGetMaterial() {
        /**
         *  "helpPicUrl": "http://static.storeer.com/wlwb/helpPicrl/04e3b2e0-1f58-46a1-9f4b-37c3e5ff869a1542592971040", // 帮助中心图片url
         *     "helpPicLink": null, // 帮助中心图片链接
         *     "videoUrl": "https://v.qq.com/x/cover/82tnyho7rqlz8z3.html" // 推手宣传视频url
         */
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == ResponseCode.RESPONSE_SUCCESS) {
                    JSONObject jsonObject = JsonUtil.getJsonObjectFromMap(response.getReturnContent());
                    try {
                        if (jsonObject.has("helpPicUrl")) {
                            String helpPicUrl = jsonObject.getString("helpPicUrl");
                        }
                        if (jsonObject.has("helpPicLink")) {
                            String helpPicLink = jsonObject.getString("helpPicLink");
                        }
                        if (jsonObject.has("videoUrl")) {
                            videoUrl = jsonObject.getString("videoUrl");
                            playVideo();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        ApiMethodGuide.getMaterialUrl(new SimpleLbObserver(listener, otherListener), this, FragmentEvent.DESTROY_VIEW);
    }
}
